/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.core;

import javax.xml.bind.annotation.XmlAttribute;

public class ReferringApplication {
	private String application;
	private CountList counts;

	public ReferringApplication() {
		super();
	}  
  
	public ReferringApplication(String application) {
		super();
		if (application == null) this.application = null;
		else this.application = application.trim();
	}  
  
	public Count addCount(String index, int referrals, String category, boolean pure) {
		for (Count aCount : this.getCount()) {
			if (aCount.getRole().equals(index)) {
				aCount.setCount(referrals);
				aCount.mergeCategory(new Count(index,referrals,category,pure));
				return aCount;
			}
		}
		if (referrals == 0) return null;
		Count aCount = new Count(index,referrals,category, pure);
		counts.add(aCount);
		return aCount;
	}

	public Count addCount(Count newCount) {
		String index = newCount.getRole();
		for (Count aCount : this.getCount()) {
			if (aCount.getRole().equals(index)) {
				aCount.setCount(newCount.getCount());
				aCount.mergeCategory(newCount);
				return aCount;
			}
		}
		if (newCount.getCount() == 0) return null;
		counts.add(newCount);
		return newCount;
	}

	/**
	 * @return the application
	 */
	@XmlAttribute(required = true)
	public String getApplication() {
		return application;
	}

	public synchronized CountList getCount() {
		if (this.counts==null) this.counts = new CountList();
		return this.counts;
	}

	public int getCountCount(String index) {
		Count aCount = getCount(index);
		if (aCount == null) return 0;
		else return aCount.getCount();
	}

	public Count getCount(String index) {
		for (Count aCount : getCount()) {
			if (aCount.getRole().equals(index)) {
				return aCount;
			}
		}
		return null;
	}

	/*
	 * If a search index exist to provide the total references available, it it will have its individual Count (index name "default" for instance)
	 * The total is therefore the maximum value of indexes (normaly the result of "default")
	 * return int, the maximum reference count
	 */
	public int getTotal() {
		if (counts == null) return 0;
		int max = 0;
		for (Count aCount : counts){
			max = (max < aCount.getCount() ? aCount.getCount() : max);
		}
		return max;
	}

	/**
	 * @param application the application to set
	 */
	public void setApplication(String application) {
		if (application == null) this.application = null;
		else this.application = application.trim();
	}	

	public void setCount(CountList indexCount) {
		this.counts = indexCount;
	}	

	public boolean putCount(CountList indexCount) {
		if (indexCount == null) return false;
		boolean modified = false;
		for (Count aCount: indexCount) {
			this.addCount(aCount);
			modified = true;
		}
		return modified;
	}	

	public String getCategory(String index) {
		for (Count aCount : getCount()) {
			if (aCount.getRole().equals(index)) {
				return aCount.getCategory();
			}
		}
		return null;
	}

	public boolean isPure(String index) {
		for (Count aCount : getCount()) {
			if (aCount.getRole().equals(index)) {
				return aCount.isPure();
			}
		}
		return false;
	}

	public String toString() {
		return application+": "+(counts==null?"null":counts.toString());
	}
}
